package searchteam.client.ui.widgets;

import java.util.ArrayList;
import java.util.Collection;

import searchteam.client.beansMirrors.PaperMirror;
import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.beansMirrors.TeamMirror;
import searchteam.client.services.SearchTeamServices;
import searchteam.client.services.SearchTeamServicesAsync;
import searchteam.client.ui.callbacks.LinkPapersToPersonCallback;
import searchteam.client.ui.callbacks.UpdatePersonCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UpdatePersonPopUp extends DialogBox{
	
	private VerticalPanel papersPanel;
	private Collection<PaperMirror> chosenPapers;
	private PaperListBox papersLB;
	private TeamListBox teamsLB;
	
	SearchTeamServicesAsync services = GWT.create(SearchTeamServices.class);

	public UpdatePersonPopUp(final Display display, final PersonMirror person) {
		
		chosenPapers = new ArrayList<PaperMirror>();
		
		this.setTitle("Modification de Personne");
		this.setText("Modification de Personne");
		this.setPopupPosition(100, 100);
		
		Grid panel = new Grid(8,2);
		
		final TextBox nameTB = new TextBox();
		nameTB.setText(person.getName());
		
		final TextBox firstNameTB = new TextBox();
		firstNameTB.setText(person.getFirstname());
		
		final TextBox telTB = new TextBox();
		telTB.setText(person.getPhone());
		
		final TextBox mailTB = new TextBox();
		mailTB.setText(person.getMail());
		
		HorizontalPanel papersAddPanel = new HorizontalPanel();
		papersLB = new PaperListBox();
		
		Button buttonAddPaper = new Button("+");
		buttonAddPaper.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addPaper(papersLB.getSelectedPaper());
			}
		});
		papersAddPanel.add(papersLB);
		papersAddPanel.add(buttonAddPaper);
		
		TeamMirror team = person.getTeam();
		
		if(team != null) {
			teamsLB = new TeamListBox(" - aucune - ", team);
		}
		else {
			teamsLB = new TeamListBox(" - aucune - ");
		}
		
		papersPanel = new VerticalPanel();
		
		ScrollPanel scrollPapers = new ScrollPanel(papersPanel);
		scrollPapers.setHeight("90px");
		
		Button buttonOK = new Button("Enregistrer");
		buttonOK.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				person.setName(nameTB.getText());
				person.setFirstname(firstNameTB.getText());
				person.setPhone(telTB.getText());
				person.setMail(mailTB.getText());
				person.setPapers(chosenPapers);
				
				TeamMirror team = teamsLB.getSelectedTeam(); 
				person.setTeam(team);
				
				services.updatePerson(person, new UpdatePersonCallback());
				
				services.linkPapersToPerson(chosenPapers, person, new LinkPapersToPersonCallback());
				
				display.getMainUI().display(new PersonMirrorDisplay(display,person));
				UpdatePersonPopUp.this.hide();
			}
		});
		Button buttonKO = new Button("Annuler");
		buttonKO.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UpdatePersonPopUp.this.hide();
			}
		});
		
		panel.setWidget(0, 0, new Label("Nom:"));
		panel.setWidget(0, 1, nameTB);
		panel.setWidget(1, 0, new Label("Prenom:"));
		panel.setWidget(1, 1, firstNameTB);
		panel.setWidget(2, 0, new Label("Tel:"));
		panel.setWidget(2, 1, telTB);
		panel.setWidget(3, 0, new Label("Mail:"));
		panel.setWidget(3, 1, mailTB);
		panel.setWidget(4, 0, new Label("Papiers :"));
		panel.setWidget(4, 1, new Label("Equipe :"));
		panel.setWidget(5, 0, papersAddPanel);
		panel.setWidget(5, 1, teamsLB);
		panel.setWidget(6, 0, scrollPapers);
		panel.setWidget(7, 0, buttonOK);
		panel.setWidget(7, 1, buttonKO);
		
		for(PaperMirror p : person.getPapers()) {
			chosenPapers.add(p);
		}
		
		refresh();
		
		this.add(panel);
		this.show();
	}
	
	public void refresh() {
		papersPanel.clear();
		for(PaperMirror p : chosenPapers) {
			
			HorizontalPanel pan = new HorizontalPanel();
			Button del = new Button("X");
			del.addClickHandler(new RemovePaperClickHandler(p));
			pan.add(del);
			pan.add(new Label(p.getTitle()));
			papersPanel.add(pan);
			
		}
	}
	
	public void addPaper(PaperMirror p) {
		chosenPapers.add(p);
		refresh();
	}
	
	class RemovePaperClickHandler implements ClickHandler {

		private PaperMirror paperMirror;
		
		public RemovePaperClickHandler(PaperMirror mirror) {
			this.paperMirror = mirror;
		}
		
		public void onClick(ClickEvent event) {
			chosenPapers.remove(paperMirror);
			refresh();
		}
		
	}
}
